home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / wb / czesc_4 / vark15 / utils p-z / retire.lha / retire / Retire.c < prev    next >
C/C++ Source or Header  |  1995-07-31  |  6KB  |  286 lines

  1.  
  2. /* RETIRE   Remove entries from User-Startup                         ***RGR***
  3.                                                                      01-may-95
  4.    Author:  Ralf Gruner, An der Sense 5a, D-02779 Großschönau, Germany.
  5.  
  6.    MANX Aztec C 5.2:
  7.    cc retire
  8.    ln retire -lc
  9. */
  10.  
  11. //#define DEBUG
  12.  
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17. #define EOL 10
  18.  
  19. char *filename1="S:User-Startup", *filename2="RAM:User-Startup";
  20. char *SalvageMessage="\nYour file \"S:User-Startup\" may be damaged.\nYou can found a copy of it in your Ram Disk.\nPlease save it NOW to a disk!\n";
  21. char *RAMerrorMsg="Error in RAM-Disk.";
  22. FILE *infile=0, *outfile=0;
  23. char *help="\nRETIRE removes entries from User-Startup.\nUsage: retire <Application 1> [<Application 2> ... [<Application n>]...]\n\n";
  24. char *version="$VER: RETIRE 1.2 (01.05.95)";
  25. char *application;
  26. int i,j,k,l;
  27. char line[256], *lineptr;
  28. int c=0;
  29.  
  30. long ftell();
  31.  
  32. /*************/
  33. void CloseAndExit(char *Msg)
  34. {
  35.     if (infile)    fclose(infile);
  36.     if (outfile)    fclose(outfile);
  37.     if (Msg!=0L)
  38.     {
  39.             puts(Msg);
  40.             exit(10);
  41.     }
  42.     remove(filename2);
  43.     exit(0);
  44. }
  45.  
  46. /*************/
  47. void ReadTextLine(void)
  48. {
  49.     for(k=0; k<256; k++)
  50.         line[k]='\0';    // clear line buffer
  51.  
  52.     k=0;
  53.     while((c=fgetc(infile))!=EOF)    // read line
  54.     {
  55.         line[k++]=c;
  56.  
  57.         #ifdef DEBUG
  58.             if(c==EOL) printf("¶");
  59.             printf("%c",c);
  60.         #endif
  61.  
  62.         if(c==EOL || c==EOF || k>=256) break;
  63.     }
  64. }
  65.  
  66. /*************/
  67. void SkipSpaces(void)
  68. {
  69.     while((*lineptr==' ' || *lineptr=='\t') && lineptr<&line[255])
  70.         lineptr++;
  71. }
  72.  
  73. /*************/
  74. main(int argc, char *argv[])
  75. {
  76.     long beginpos;
  77.     int ok=0, EmptyLine=0, AlreadyEmptyLine=0;
  78.  
  79.     if (*argv[1] == '?')
  80.     {
  81.         printf("\nRETIRE v%s   Author: Ralf Gruner, Großschönau, Germany.\n",version+13);
  82.         printf(help);
  83.         exit(0);
  84.     }
  85.  
  86.     if (argc < 2)
  87.     {
  88.         printf("\nMissing application name.\n");
  89.         CloseAndExit(help);
  90.     }
  91.     else
  92.     {
  93.         for(i=1; i<=argc-1; i++)
  94.         {
  95.             #ifdef DEBUG
  96.                 printf("\nPass %d: %s\n",i,argv[i]);
  97.             #endif
  98.  
  99.             if(!(infile=fopen(filename1,"r")))
  100.             {
  101.                 printf("%s does not exist.\n",filename1);
  102.                 CloseAndExit(0L);
  103.             }
  104.  
  105.             if(!(outfile=fopen(filename2,"w")))
  106.             {
  107.                 printf("\nCould not open %s.\n",filename2);
  108.                 CloseAndExit("");
  109.             }
  110.  
  111.             while((c=fgetc(infile))!=EOF)    // copy s:user-startup into ram-disk
  112.             {
  113.                 if(fputc(c, outfile)==EOF)
  114.                     CloseAndExit("\nError while writing.\n");
  115.             }
  116.  
  117.             if(fclose(infile)==0)
  118.                 infile=0;
  119.             else
  120.             {
  121.                 printf("\nCould not close %s.\n",filename1);
  122.                 CloseAndExit("");
  123.             }
  124.             if(fclose(outfile)==0)
  125.                 outfile=0;
  126.             else
  127.             {
  128.                 printf("\nCould not close %s.\n",filename2);
  129.                 CloseAndExit("");
  130.             }
  131.  
  132.  
  133.             if(!(infile=fopen(filename2,"r")))
  134.             {
  135.                 printf("\nCould not open %s.\n",filename2);
  136.                 CloseAndExit("");
  137.             }
  138.  
  139.             application=argv[i];    // application name
  140.             c=0;
  141.             ok=0;
  142.  
  143.             while(c!=EOF && ok==0)    // search current application name
  144.             {
  145.                 ReadTextLine();
  146.                 ok=0;
  147.                 if(strnicmp(&line[0],";BEGIN ",7)==0)
  148.                 {
  149.                     lineptr=&line[7];
  150.                     SkipSpaces();
  151.                     if(strnicmp(lineptr,application,strlen(application))==0)
  152.                     {
  153.                         lineptr+=strlen(application);
  154.                         SkipSpaces();
  155.                         if(*lineptr==EOL)
  156.                         {
  157.                             ok=1;    // found application name
  158.                         }
  159.                     }
  160.                 }
  161.             }
  162.  
  163.             if(0!=fseek(infile,0,0)) CloseAndExit(RAMerrorMsg);
  164.  
  165.             #ifdef DEBUG
  166.                 if(ok==1)    printf("Found application \"%s\".\n",application);
  167.             #endif
  168.  
  169.             if(ok==1)
  170.             {
  171.                 // if application name exists, then write all lines without application entry
  172.  
  173.                 if(!(outfile=fopen(filename1,"w")))
  174.                 {
  175.                     printf("\nCould not open %s for writing.\n",filename1);
  176.                     CloseAndExit("");
  177.                 }
  178.  
  179.                 c=0;
  180.                 ok=0;
  181.     
  182.                 while(c!=EOF)
  183.                 {
  184.                     beginpos=ftell(infile);
  185.                     ReadTextLine();
  186.                     ok=0;
  187.                     if(strnicmp(&line[0],";BEGIN ",7)==0)
  188.                     {
  189.                         lineptr=&line[7];
  190.                         SkipSpaces();
  191.                         if(strnicmp(lineptr,application,strlen(application))==0)
  192.                         {
  193.                             lineptr+=strlen(application);
  194.                             SkipSpaces();
  195.                             if(*lineptr==EOL)
  196.                             {
  197.                                 ok=1;
  198.                                 while(strnicmp(&line[0],";END ",5)!=0 && c!=EOF)
  199.                                 {
  200.                                     ReadTextLine();
  201.                                     if(c==EOF)
  202.                                     {
  203.                                         ok=0;
  204.                                         break;
  205.                                     }
  206.                                 }
  207.                                 if(ok==1)
  208.                                 {
  209.                                     // found ;END
  210.                                     lineptr=&line[5];
  211.                                     SkipSpaces();
  212.                                     if(strnicmp(lineptr,application,strlen(application))==0)
  213.                                     {
  214.                                         // found name
  215.                                         lineptr+=strlen(application);
  216.                                         SkipSpaces();
  217.                                         if(*lineptr==EOL)
  218.                                         {
  219.                                             ok=1;
  220.                                         }
  221.                                         else
  222.                                         {
  223.                                             ok=0;
  224.                                         }
  225.                                     }
  226.                                     else
  227.                                         ok=0;
  228.                                 }
  229.                                 if(ok==0)
  230.                                 {
  231.                                     printf("\nError in structure of S:User-Startup:\n;BEGIN %s  does not match  %s\n",application,&line[0]);
  232.                                     if(0!=fseek(infile,beginpos,0))
  233.                                         puts(RAMerrorMsg);
  234.                                     ReadTextLine();    // if END not valid, read BEGIN again
  235.                                 }
  236.                                 if(ok==1)
  237.                                 {
  238.                                     #ifdef DEBUG
  239.                                         printf("(*** removed.***)\n");
  240.                                     #endif
  241.                                     printf("%s retired.\n",application);
  242.                                     continue;
  243.                                 }
  244.                             }
  245.                         }
  246.                     }
  247.  
  248.                     lineptr=&line[0];
  249.                     SkipSpaces();
  250.                     if(*lineptr==EOL)
  251.                         EmptyLine=1;
  252.                     else
  253.                         EmptyLine=0;
  254.                     if(AlreadyEmptyLine==0 || EmptyLine==0)
  255.                     {
  256.                         l=0;
  257.                         while(l<k)    // write line
  258.                         {
  259.                             c=line[l++];
  260.                             if(fputc(c, outfile)==EOF)
  261.                             {
  262.                                 printf("\nError while writing %s.%s",filename1,SalvageMessage);
  263.                                 CloseAndExit("");
  264.                             }
  265.                         }
  266.                     }
  267.                     AlreadyEmptyLine=EmptyLine;
  268.                 }
  269.                 if(fclose(outfile)==0)
  270.                     outfile=0;
  271.                 else
  272.                 {
  273.                     printf("\nCould not close %s.%s",filename1,SalvageMessage);
  274.                     CloseAndExit("");
  275.                 }
  276.             }
  277.             if(fclose(infile)==0)
  278.                 infile=0;
  279.             else
  280.                 CloseAndExit(RAMerrorMsg);
  281.         }
  282.     }
  283. CloseAndExit(0L);
  284. }
  285.  
  286.